#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2019/7/25 下午8:42
# @Author  : fugang_le
# @Software: PyCharm



def euclidean_distance():
    '''
    欧式距离
    :return:
    '''
    import numpy as np

    vector1 = np.array([1, 2, 3])
    vector2 = np.array([4, 5, 6])

    op1 = np.sqrt(np.sum(np.square(vector1 - vector2))) # 方法1
    # 求2阶范数
    op2 = np.linalg.norm(vector1 - vector2) #方法2  np.linalg == linear(线性) + algebra(代数)


def manhattan_distance():
    '''
    曼哈顿距离
    :return:
    '''
    import numpy as np
    vector1 = np.array([1, 2, 3])
    vector2 = np.array([4, 5, 6])

    op3 = np.sum(np.abs(vector1 - vector2))
    op4 = np.linalg.norm(vector1 - vector2, ord=1) # ord参数表示一阶范数


def cosine():
    '''
    余弦夹角
    :return:
    '''
    import numpy as np

    vector1 = np.array([1, 2, 4])
    vector2 = np.array([1, 2, 3])

    op7 = np.dot(vector1, vector2) / (np.linalg.norm(vector1) * (np.linalg.norm(vector2)))
    return op7


def hamming_distance():
    '''
    汉明距离
    :return:
    '''
    import numpy as np

    v1 = np.array([1, 1, 0, 1, 0, 1, 0, 0, 1])
    v2 = np.array([0, 1, 1, 0, 0, 0, 1, 1, 1])
    smstr = np.nonzero(v1 - v2)
    print(smstr)    # 不为0 的元素的下标
    sm = np.shape(smstr[0])[0]
    print(sm)


def jaccard_distance():
    '''
    杰卡德距离
    :return:
    '''

    import scipy.spatial.distance as dist
    import numpy as np

    v1 = np.array([1, 1, 0, 1, 0, 1, 0, 0, 1])
    v2 = np.array([0, 1, 1, 0, 0, 0, 1, 1, 1])

    matv = np.array([v1, v2])
    print(matv)
    ds = dist.pdist(matv, 'jaccard')
    print(ds)


